import { Component, OnInit, Input } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { MdlSnackbarService, MdlDialogService } from '@angular-mdl/core';
import { CustomService } from '../../custom.service'

@Component({
    selector: 'custom-baseinfo',
    templateUrl: './baseinfo.component.html',
    styleUrls: ['./baseinfo.component.scss'],
    providers: []
})
export class BaseInfoComponent implements OnInit {
    currentItemId: number = 0;
    configItem: any = {};
    configForm: FormGroup;

    @Input()
    set itemId(itemId: number) {
        if (itemId) {
            this.currentItemId = itemId;
            this.customService.loadCustomBizItem(this.currentItemId).subscribe(item => {
                this.configItem = item;
                this.buildForm();
            });
        }
    }
    get itemId(): number { return this.currentItemId; }

    constructor(
        private fb: FormBuilder,
        private customService: CustomService,
        private mdlDialogService: MdlDialogService,
        private mdlSnackbarService: MdlSnackbarService) {
    }
    ngOnInit() {
        this.buildForm();
    }

    saveUpdate() {
        this.customService.updateCustomBiz(this.configForm.value).subscribe(data => {
            this.customService.announceMission(this.currentItemId);
            this.mdlSnackbarService.showToast("保存成功!");
        });
    }

    delete() {
        this.mdlDialogService.confirm("您确定要删除当前选中业务吗？", "取消", "确定").subscribe(() => {
            this.customService.deleteCustomItem(this.currentItemId).subscribe(data => {
                this.customService.announceMission(this.currentItemId);
                this.mdlSnackbarService.showToast("删除成功!");
            });
        });
    }

    buildForm(): void {
        this.configForm = this.fb.group({
            'itemId': [this.configItem.itemId],
            'itemName': [this.configItem.itemName],
            'itemIcon': [this.configItem.itemIcon],
            'bizId': [this.configItem.bizId],
            'bizName': [this.configItem.bizName],
            'bizIcon': [this.configItem.bizIcon],
            'procDefId': [this.configItem.procDefId],
            'formDefId': [this.configItem.formDefId],
            'taskListGroupId': [this.configItem.taskListGroupId],
            'iconCss': [this.configItem.iconCss],
            'createHuman': [this.configItem.createHuman],
            'createTime': [this.configItem.createTime],
            'displayOrder': [this.configItem.displayOrder],
            'groupName': [this.configItem.groupName],
            'tableName': [this.configItem.tableName],
            'enableFlag': [this.configItem.enableFlag]
        });
    }
}
